'use strict';

const Controller = require('egg').Controller;

class TestController extends Controller {
  async getTest() {
    const result = await this.ctx.curl('106.15.191.76/examiantion/userTestList', {
      method: 'POST',
      contentType: 'json',
      data: {
        email: this.ctx.session.email,
      },
      dataType: 'json',
    });
    const returnList = [];
    for (const i in result.data) {
      returnList.push({
        id: result.data[i].testPaperID.id,
        status: result.data[i].status === 'no_start' ? '待完成' : result.data[i].score,
        name: result.data[i].testPaperID.name,
        beginTime: new Date(result.data[i].testPaperID.beginTime).toLocaleString(),
        endTime: new Date(result.data[i].testPaperID.endTime).toLocaleString(),
        totalScore: result.data[i].testPaperID.totalScore,
      });
    }
    this.ctx.body = returnList;
    this.ctx.status = 200;
  }

  async getQuestions() {
    const result = await this.ctx.curl('106.15.191.76/examination/paperproblemList', {
      method: 'POST',
      contentType: 'json',
      data: {
        name: this.ctx.request.query.name,
      },
      dataType: 'json',
    });
    for (const i in result.data) {
      if (result.data[i].problem.type === 1) {
        result.data[i].problem.type = '单选题';
      }
    }
    this.ctx.body = result.data;
    this.ctx.status = 200;
  }

  async scoring() {
    console.log({
      userID: this.ctx.session.user_id.toString(),
      testPaperID: this.ctx.request.body.testPaperID,
      answers: this.ctx.request.body.answers,
    });
    const result = await this.ctx.curl('106.15.191.76/examination/computeScore', {
      method: 'POST',
      contentType: 'json',
      data: {
        userID: this.ctx.session.user_id.toString(),
        testPaperID: this.ctx.request.body.testPaperID,
        answers: this.ctx.request.body.answers,
      },
      dataType: 'json',
    });
    console.log(result);
    this.ctx.body = result.data;
    this.ctx.status = 200;
  }
}

module.exports = TestController;
